/* Hawk - An OSGI Based Test Framework
 *
 * Copyright (C) 2010 http://code.google.com/p/hawk-framework/
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.hawk.bundles.ping;

import java.net.InetAddress;
import java.util.List;
import java.util.logging.Logger;

import org.hawk.core.interfaces.TestService;
import org.hawk.core.model.Input;
import org.hawk.core.model.Parameter;

/**
 * This class is responsible to verify a host connectivity
 * 
 * @author Rodrigo Prestes Machado
 * @version July 8, 2010
 */
public class Ping implements TestService {

	/**
	 * @return Return 1 for true and 0 for false in string format
	 */
	@Override
	public String execute(Input input, Logger logger) throws Exception {
		
		List<Parameter> parameters = (List<Parameter>) input.getParameters();
		Parameter parameter = parameters.get(0);
		
		InetAddress address = InetAddress.getByName(parameter.getValue());
		
		String result = "0";
		if (address.isReachable(10)){
			result = "1";
		}
		
		return result ;
	}

	/**
	 * Bundle interface
	 */
	@Override
	public String getBundleInterface() throws Exception {
		String bundleGui = "<?xml version='1.0' encoding='UTF-8'?>" +
        "<bundle-gui name='org.soatest.bundles.hello.Hello'>" +
           	"<input>" +
                "<parameter label='Host:' description='A ip ou name of a host' />" +
            "</input>" +
        "</bundle-gui>";
		
		return bundleGui;
	}

}